package com.weiyi.mashangzhu.Roomer.Fragment;

import android.Manifest;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Landlord.Activity.MainActivity_L;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.AboutActivity;
import com.weiyi.mashangzhu.Roomer.Activity.CollectActivity;
import com.weiyi.mashangzhu.Roomer.Activity.CouponActivity;
import com.weiyi.mashangzhu.Roomer.Activity.HelpActivity;
import com.weiyi.mashangzhu.Roomer.Activity.IdeaActivity;
import com.weiyi.mashangzhu.Roomer.Activity.IdentityActivity;
import com.weiyi.mashangzhu.Roomer.Activity.LoginActivity;
import com.weiyi.mashangzhu.Roomer.Activity.MainActivity_R;
import com.weiyi.mashangzhu.Roomer.Activity.PersonInfoActivity;
import com.weiyi.mashangzhu.Roomer.Activity.ShareActivity;
import com.weiyi.mashangzhu.Roomer.Activity.SysMsgActivity;
import com.weiyi.mashangzhu.Roomer.Activity.WalletCenterActivity;
import com.weiyi.mashangzhu.Roomer.Bean.PersonInfoBean;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by win7 on 2017/8/18.
 */

public class Fragment_Person extends Fragment {
    @BindView(R.id.person_id)
    TextView personId;
    @BindView(R.id.person_head)
    ImageView personHead;
    Unbinder unbinder;
    TextView pop_contact_phone,pop_off;
    private final String TAG = "MPermissions";
    private int REQUEST_CODE_PERMISSION = 0x00099;
    private MainActivity_R mainActivity;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity = (MainActivity_R) getActivity();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
            personHead.setImageResource(R.mipmap.logo);
            personId.setText("未登录");

        } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
            OkHttpUtils.get()
                    .addParams("token", SPUtils.get(mainActivity, "token", "") + "")
                    .url(API.BaseApi_R + API.PersonInfo_Api_R)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            T.showShort(mainActivity, "网络连接失败，请检查一下网络设置");
                        }

                        @Override
                        public void onResponse(String response) {

                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                if (jsonObject.getInt("status") == 1) {
                                    T.showShort(mainActivity, jsonObject.getString("msg"));
                                    SPUtils.put(mainActivity, "isLogin", "0");
                                } else {
                                    PersonInfoBean personInfoBean = new Gson().fromJson(response, PersonInfoBean.class);
                                    if (mainActivity!=null&&personHead!=null){

                                        GlideImgManager.glideLoader(mainActivity, API.BaseApi_url + personInfoBean.getData().getUHead(), R.mipmap.logo, personHead, 0);
                                    }

                                    if (personInfoBean.getData().getUNickname() == null || personInfoBean.getData().getUNickname().equals("")) {
                                        personId.setText(personInfoBean.getData().getUAccount().substring(0, 3) + "****" + personInfoBean.getData().getUAccount().substring(7));
                                    }
                                    personId.setText(personInfoBean.getData().getUNickname() + "");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
        }
        }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        unbinder.unbind();
    }
    public void showPopupWindow() {
        View contentView = LayoutInflater.from(mainActivity).inflate(R.layout.pop_win_contact, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        pop_contact_phone= (TextView) contentView.findViewById(R.id.pop_contact_phone);
        pop_off= (TextView) contentView.findViewById(R.id.pop_off);
        pop_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        pop_contact_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                   mainActivity.requestPermission(new String[]{Manifest.permission.CALL_PHONE}, 0x004);
                    Log.e("hhhhhhhhhhhhhhhhh","1");
                } else {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + "4000000000"));
                    startActivity(intent);
                    Log.e("hhhhhhhhhhhhhhhhh","1");
                }
            }
        });
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.popup_window_bottom_animation);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x55000000));
        // 设置好参数之后再show
        if (popupWindow.isShowing()){

        }else {
            popupWindow.showAsDropDown(contentView);
        }

    }


    @OnClick({R.id.inform,R.id.person_head,R.id.person_change, R.id.person_coupon, R.id.person_collect, R.id.person_msg, R.id.person_friend, R.id.person_contact, R.id.person_idea, R.id.person_help, R.id.person_about, R.id.person_identity, R.id.person_wallet})
    public void onViewClicked(View view) {
        final Intent[] intent = {null};
        switch (view.getId()) {
            case R.id.inform:
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    UiUtil.startActivity(mainActivity, LoginActivity.class);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    intent[0] =new Intent(mainActivity, SysMsgActivity.class);
                    mainActivity.startActivity(intent[0]);
                }


                break;
            case R.id.person_head:
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    intent[0] =new Intent(mainActivity, LoginActivity.class);
                    intent[0].putExtra("string","变身房客");
                    startActivity(intent[0]);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    //个人资料
                    intent[0] =new Intent(mainActivity, PersonInfoActivity.class);
                    mainActivity.startActivity(intent[0]);
                }

                break;
            case R.id.person_change:
                    //变身房东
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    intent[0] =new Intent(mainActivity, LoginActivity.class);
                    intent[0].putExtra("string","变身房客");
                    startActivity(intent[0]);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    OkHttpUtils.get()
                            .addParams("token",SPUtils.get(mainActivity,"token","")+"")
                            .url(API.BaseApi_R+API.Change_Api)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Request request, Exception e) {
                                    T.showShort(mainActivity,"数据获取失败，请稍后再试");
                                }

                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonObject=new JSONObject(response);
                                        if (jsonObject.getInt("status")==0){
                                            intent[0] =new Intent(mainActivity, MainActivity_L.class);
                                            mainActivity.startActivity(intent[0]);
                                        }
                                        T.showShort(mainActivity,jsonObject.getString("msg"));
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                            });

                }
                break;
            case R.id.person_coupon:
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    intent[0] =new Intent(mainActivity, LoginActivity.class);
                    intent[0].putExtra("string","变身房客");
                    startActivity(intent[0]);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    //优惠券
                    intent[0] =new Intent(mainActivity, CouponActivity.class);
                    intent[0].putExtra("info","personinfo");
                    mainActivity.startActivity(intent[0]);
                }

                break;
            case R.id.person_collect:
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    intent[0] =new Intent(mainActivity, LoginActivity.class);
                    intent[0].putExtra("string","变身房客");
                    startActivity(intent[0]);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    //收藏
                    intent[0] =new Intent(mainActivity, CollectActivity.class);
                    mainActivity.startActivity(intent[0]);
                }

                break;
            case R.id.person_msg:
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    intent[0] =new Intent(mainActivity, LoginActivity.class);
                    intent[0].putExtra("string","变身房客");
                    startActivity(intent[0]);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    //系统消息
                    intent[0] =new Intent(mainActivity, SysMsgActivity.class);
                    mainActivity.startActivity(intent[0]);
                }

                break;
            case R.id.person_friend:
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    intent[0] =new Intent(mainActivity, LoginActivity.class);
                    intent[0].putExtra("string","变身房客");
                    startActivity(intent[0]);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    //邀请好友
                    intent[0] =new Intent(mainActivity, ShareActivity.class);
                    mainActivity.startActivity(intent[0]);
                }

                break;
            case R.id.person_contact:
               //联系客服
                showPopupWindow();
                break;
            case R.id.person_idea:
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    intent[0] =new Intent(mainActivity, LoginActivity.class);
                    intent[0].putExtra("string","变身房客");
                    startActivity(intent[0]);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    //意见反馈
                    intent[0] =new Intent(mainActivity, IdeaActivity.class);
                    mainActivity.startActivity(intent[0]);
                }

                break;
            case R.id.person_help:
                //帮助
                intent[0] =new Intent(mainActivity, HelpActivity.class);
                mainActivity.startActivity(intent[0]);
                break;
            case R.id.person_about:
                //关于马上住
                intent[0] =new Intent(mainActivity, AboutActivity.class);
                mainActivity.startActivity(intent[0]);
                break;
            case R.id.person_identity:
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    intent[0] =new Intent(mainActivity, LoginActivity.class);
                    intent[0].putExtra("string","变身房客");
                    startActivity(intent[0]);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    //实名认证
                    intent[0] =new Intent(mainActivity, IdentityActivity.class);
                    mainActivity.startActivity(intent[0]);
                }
                break;
            case R.id.person_wallet:
                if (SPUtils.get(mainActivity, "isLogin", "0").equals("0")) {
                    intent[0] =new Intent(mainActivity, LoginActivity.class);
                    intent[0].putExtra("string","变身房客");
                    startActivity(intent[0]);
                } else if (SPUtils.get(mainActivity, "isLogin", "0").equals("1")) {
                    //账户中心
                    intent[0] =new Intent(mainActivity, WalletCenterActivity.class);
                    intent[0].putExtra("info","r");
                    mainActivity.startActivity(intent[0]);
                }

                break;
        }
    }
}
